﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Frogism.Core;
using NHibernate;

namespace Frogism.Data._2
{
    public class UserRepository
    {
        public void Update(User user)
        {
            using (NHibernate.ISession session = NHConfig.GetSessionFactory().OpenSession())
            {
                using (NHibernate.ITransaction trans = session.BeginTransaction())
                {

                    session.Update(user);
                    trans.Commit();
                }
            }
        }
        public Frogism.Core.User GetOne(int id)
        {
            using (NHibernate.ISession session = NHConfig.GetSessionFactory().OpenSession())
            {
                return session.Get<User>(id);
            }
        }
        public Frogism.Core.User GetByName(string name)
        {
            using (NHibernate.ISession session = NHConfig.GetSessionFactory().OpenSession())
            {
                var userList = session.CreateQuery("from User u where u.Name=:Name").SetString("Name", name).List<Core.User>();
                if (userList.Count >= 1)
                {
                    return userList[0];
                }
                else
                {
                    return null;
                }
            }
         
        }
    }
}
